<?php
if ( function_exists('register_sidebar') )
    register_sidebar(array(
        'before_widget' => '',
        'after_widget' => '</li>',
        'before_title' => '<li><h2>',
        'after_title' => '</h2></li><li>',
    ));

add_action('admin_menu', 'Imagination_add_theme_page');

function ml_input( $var, $type, $description = "", $value = "", $selected="" ) {

	// ------------------------
	// add a form input control
	// ------------------------
	
 	echo "\n";
 	
	switch( $type ){
	
	    case "text":

	 		echo "<input name=\"$var\" id=\"$var\" type=\"$type\" style=\"width: 60%\" class=\"textbox\" value=\"$value\" />";
			
			break;
			
		case "submit":
		
	 		echo "<p class=\"submit\"><input name=\"$var\" type=\"$type\" value=\"$value\" /></p>";

			break;

		case "option":
		
			if( $selected == $value ) { $extra = "selected=\"true\""; }

			echo "<option value=\"$value\" $extra >$description</option>";
		
		    break;

	}

}

function Imagination_add_theme_page() {
	if ( $_GET['page'] == basename(__FILE__) ) {
	
	    // save settings
		if ( 'save' == $_REQUEST['action'] ) {

			update_option( 'home_page_post_number', $_POST['home_page_post_num'] );
			update_option( 'recent_post_number', $_POST['recent_post_num'] );
			update_option( 'aside_post_cat_id', $_POST['aside_cat_id'] );
			update_option( 'aside_post_number', $_POST['aside_post_num'] );

			header("Location: themes.php?page=functions.php&saved=true");
			die;

  		// reset settings
		} else if( 'reset' == $_REQUEST['action'] ) {

			delete_option( 'home_page_post_number' );
			delete_option( 'recent_post_number' );
			delete_option( 'aside_post_cat_id' );
			delete_option( 'aside_post_number' );
			
			
			// goto theme edit page
			header("Location: themes.php?page=functions.php&reset=true");
			die;

		}
	}


    add_theme_page("Imagination Options", "Imagination Options", 'edit_themes', basename(__FILE__), 'Imagination_theme_page');

} 

function Imagination_theme_page() {

	// --------------------------
	// VideoDen theme page content
	// --------------------------

	if ( $_REQUEST['saved'] ) echo '<div id="message" class="updated fade"><p><strong>Imagination Theme: Settings saved.</strong></p></div>';
	if ( $_REQUEST['reset'] ) echo '<div id="message" class="updated fade"><p><strong>Imagination Theme: Settings reset.</strong></p></div>';
	
?>

    <div class="wrap">
        <h2>Homepage Options:</h2>
        <form method="post" action="">
<?php

if ($_POST['home_page_post_num'] AND !preg_match("/[^0-9]+$/ ",$_POST['home_page_post_num'])) {
        update_option('home_page_post_number', $_POST['home_page_post_num']);
        } ?>

Number of posts to display on the homepage: <input name="home_page_post_num" id="home_page_post_num" type="text" value="<?php echo(get_option('home_page_post_number')); ?>" size="2" /><br/><br/>

<?php

if ($_POST['recent_post_num'] AND !preg_match("/[^0-9]+$/ ",$_POST['recent_post_num'])) {
        update_option('recent_post_number', $_POST['recent_post_num']);
        } ?>

Number of additional recent posts to display: <input name="recent_post_num" id="recent_post_num" type="text" value="<?php echo(get_option('recent_post_number')); ?>" size="2" /><br/><br/>

<?php

if ($_POST['aside_cat_id']) {
        update_option('aside_post_cat_id', $_POST['aside_cat_id']);
    }
    $cats = get_categories();

?>
        Category to display in Asides: <select name="aside_cat_id">
            <?php
            foreach ($cats as $cat) {
            $option = '<option value="'.$cat->cat_ID.'"';
            if ($cat->cat_ID == get_option('aside_post_cat_id')) { $option .= ' SELECTED >'; } else { $option .= ">"; }
            $option .= $cat->cat_name;
            $option .= '</option>';
            echo $option; }
            ?>
        </select><br/><br/>

<?php

if ($_POST['aside_post_num'] AND !preg_match("/[^0-9]+$/ ",$_POST['aside_post_num'])) {
        update_option('aside_post_number', $_POST['aside_post_num']);
        } ?>

Number of aside posts to display (on the left): <input name="aside_post_num" id="aside_post_num" type="text" value="<?php echo(get_option('aside_post_number')); ?>" size="2" /><br/><br/>

<input type="hidden" name="action" value="save" />
	<?php ml_input( "save", "submit", "", "Save Settings" );?>     </form><br/><br/>

<form method="post">

<h2>Reset</h2>

<p>If for whatever reason you want to "clean up" the settings set here or want to use another theme, click the <em>Reset Settings</em> button below.  To completely remove the theme, make sure to delete the <em>/wp-imagination/</em> folder in the <em>/wp-content/themes/</em> directory.</p>
<?php

	ml_input( "reset", "submit", "", "Reset Settings" );
	
?>

<input type="hidden" name="action" value="reset" />
</form>

<h2>Support</h2>

<p>If you need any support with this particular theme, feel free to post your question in the <a href="http://www.themelab.com/forums/forum/wp-imagination">WP Imagination support forum</a> over at <a href="http://www.themelab.com">Theme Lab</a>.</p>

</div>
  <?php } ?>